/**
 * 给定一个字符串Target
 * 给定Q个字符串Pattern
 * 每一次问第i个Pattern在Target中出现的次数
 * 标准的AC自动机。也可以用SA。
 * 注意去重。在Fail树上打标记，最后做一个遍历。
 */
#include <bits/stdc++.h>
using namespace std;
#include <bits/extc++.h>
using namespace __gnu_pbds;

using llt = long long;
using vi = vector<int>;
using vll = vector<llt>;

struct Aho{

enum{ALPHA_SIZE=26}; // 字母种类

vector<array<int, ALPHA_SIZE>> Trie;
vector<int> Flag; // 记录末端对应的索引
vector<int> Fail; 
vector<vector<int>> G; // Fail树

array<int, 256> L2I;

void init(int capa=1E6){
    for(int i=0;i<ALPHA_SIZE;++i){
        L2I[i + 'a'] = i;
    }

    Trie.reserve(capa);
    Fail.reserve(capa);
    Flag.reserve(capa);
    G.reserve(capa);
    
    SCnt = 0;
    ErrorState = _newState();
    StartState = _newState();

    return;
}

template<typename IT=string::iterator>
void insert(IT s, IT e, int idx){
    int state = StartState;
    for(auto it=s;it!=e;++it){
        int sn = L2I[(int)*it];
        if(0 == Trie[state][sn]){
            Trie[state][sn] = _newState();
        }
        state = Trie[state][sn];
    }
    Flag[state] = idx;
    return;
}

void buildAC(){
    Fail[StartState] = StartState;
    queue<int> q;
    for(int i=0;i<ALPHA_SIZE;++i){
        int & state = Trie[StartState][i];
        if(state){
            Fail[state] = StartState;
            q.push(state);
            G[StartState].push_back(state);
        }else{
            state = StartState;
        }
    }

    while(not q.empty()){
        int u = q.front(); q.pop();
        for(int i=0;i<ALPHA_SIZE;++i){
            int & state = Trie[u][i];
            if(state){
                Fail[state] = Trie[Fail[u]][i];
                q.push(state);
                G[Trie[Fail[u]][i]].push_back(state);
            }else{
                state = Trie[Fail[u]][i];
            }
        }
    }

    return;
}

template<typename IT = string::iterator>
void query(IT s, IT e, std::function<void(int, int)> f){
    int state = StartState;
    for(auto it=s;it!=e;++it){
        int sn = L2I[(int)*it];
        state = Trie[state][sn];
        f(state, Flag[state]);        
    }
    return;
}

int _newState(){
    Trie.push_back({0});
    Fail.push_back(0);
    Flag.push_back({});
    G.push_back({});
    return SCnt++;
}

int StartState;
int ErrorState;
int SCnt;

};

int Q;
map<string, int> T;
string S;
Aho AC;
vector<int> D;
vector<int> Ans;
vi G;

void dfs(int u, int p){
    for(auto v : AC.G[u]){
        if(u == p) continue;
        dfs(v, u);
        D[u] += D[v];
    }
    if(AC.Flag[u]) Ans[AC.Flag[u] - 1] += D[u];
    return;
}

void work(){
    cin >> S;
    cin >> Q;
    G.assign(Q + 1, {});
    T.clear();
    for(int q=1;q<=Q;++q){
        string s; 
        cin >> s;
        auto it = T.find(s);
        if(it == T.end()){
            it = T.insert(it, {s, q});
        }
        G[q] = it->second;
    }
    
    AC.init();   
    for(const auto & p : T){
        AC.insert(p.first.begin(), p.first.end(), p.second);
    }
    AC.buildAC();

    D.assign(AC.Flag.size(), 0);
    AC.query(S.begin(), S.end(), [&](int state, int idx){D[state] += 1;});

    Ans.assign(Q, 0);
    dfs(1, 0);

    for(int i=1;i<=Q;++i) cout << Ans[G[i] - 1] << "\n";
	return;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);	
    int nofkase = 1;
	// cin >> nofkase;
	while(nofkase--) work();
	return 0;
}